﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AjaxControlToolkit;
using System.Web.UI;
using System.Diagnostics;
using System.Web.UI.WebControls;

namespace Kevin.Controls
{
    public class SitePath:WebControl
    {
        private string strPath = "";
        List<string> pathList = new List<string>();
        public SitePath(){ }

        protected override void OnPreRender(EventArgs e)
        {

            base.OnPreRender(e);
            if (string.IsNullOrEmpty(this.MenuID))
                throw new Exception("SitePath控件必须设置MenuID,如有疑问请与我(席吉波)联系.");
            CreateSitePath();
        }

        [ClientPropertyName("MenuID")]
        public string MenuID
        {
            get { return GetPropertyValue("MenuID",""); }
            set { SetPropertyValue("MenuID", value); }
        }

        private void CreateSitePath()
        {
            LeftMenu menu = (LeftMenu)base.FindControl(this.MenuID);// == null ? (LeftMenu)Page.FindControl(this.MenuID) : (LeftMenu)base.FindControl(this.MenuID);
            if (menu == null)
            {
                Literal li = new Literal();
                li.Text = (string)Page.Session["KevinSitePath"];               
                this.Controls.Add(li);
                return;
            }
            else
            {
                this.MenuID = menu.ClientID;
                string current = menu.CurrentLink;
                string strUrl = Page.Request.Url.ToString();
                Literal li = new Literal();

                GetSitePath(menu.MenuList, strUrl);
                string[] pathArray = pathList.ToArray();
                if (Page.Session["KevinSitePath"] != null && pathArray.Length == 0)
                {

                    li.Text = (string)Page.Session["KevinSitePath"];
                    base.Controls.Add(li);
                    return;
                }
                for (int i = 0; i < pathArray.Length; i++)
                {
                    if (i == pathArray.Length - 1)
                        strPath += "<font color='red'>" + pathArray[i] + "</font>";
                    else
                        strPath += pathArray[i] + " -> ";
                }
                if (strPath.Length > 0)
                {
                    li.Text = "当前路径：" + strPath;
                    this.Controls.Add(li);
                    ViewState["KevinSitePath"] = li.Text;
                    Page.Session["KevinSitePath"] = li.Text;
                }
            }
           
           
           
        }
        

        private void GetSitePath(List<MenuItem> menuList, string strUrl)
        {
            foreach (MenuItem item in menuList)
            {
                try
                {
                    if (string.IsNullOrEmpty(item.Link))
                    {
                        if (!item.HasChildMenu)
                            continue;
                        else
                            GetSitePath(item.ChildMenu, strUrl);
                    }
                    else
                    {
                        if (strUrl.Contains(ResolveUrl(item.Link)))
                        {
                            //pathList.Add(item.Text);
                            SetSitePath(item);
                            return;
                        }
                        else
                        {
                            if (item.HasChildMenu)
                                GetSitePath(item.ChildMenu, strUrl);
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message + ",++" + item.Link + "," + item.Text);
                }
            }
            //return new MenuItem();

        }
        private void SetSitePath(MenuItem p_item)
        {

            if (p_item.ParentMenu != null)
            {
                pathList.Insert(0, p_item.Text);
                SetSitePath(p_item.ParentMenu);
            }
            else
            {
                string strLink = "<a href='"+ResolveUrl(p_item.Link)+"'>"+p_item.Text+"</a>";
                pathList.Insert(0, strLink);
            }
            
        }
        #region Helper

        /// <summary>
        /// Retrieves the property value from the ViewState
        /// </summary>
        /// <typeparam name="V">Type of value</typeparam>
        /// <param name="propertyName">Property to retrieve</param>
        /// <param name="nullValue">value to be used in case it's not initialised.</param>
        /// <returns></returns>
        [DebuggerStepThrough]
        protected V GetPropertyValue<V>(string propertyName, V nullValue)
        {
            if (ViewState[propertyName] == null)
            {
                return nullValue;
            }
            return (V)ViewState[propertyName];
        }


        /// <summary>
        /// Save the property value in the ViewState
        /// </summary>
        /// <typeparam name="V">Property type</typeparam>
        /// <param name="propertyName">Property name</param>
        /// <param name="value">Value</param>
        [DebuggerStepThrough]
        protected void SetPropertyValue<V>(string propertyName, V value)
        {
            ViewState[propertyName] = value;
        }

        #endregion
    }
}
